/**

 */

var metaXHR;


var main;
var title;
var date;
var thumb;
var desc;

var episode;

var nowPlaying;
var playDate;
var playDesc;

var cursorPosition = 1;

var searchString;

var embedStorage = "embedChromeless";
var embedVideoId;

// XML Namespace resolver for XPath namespace resolution
function NSResolver(prefix) 
{
	if(prefix == 'media') 
	{
		return "http://search.yahoo.com/mrss/";
	}
	return null;
}

// Takes a definition document and populates the various DCTK controls
function handleMetaResult(doc)
{
    // extract information with XPath
	
	episode = doc.evaluate("//video/title", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
	main = doc.evaluate("//show/name", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
	mainDesc = doc.evaluate("//video/description", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
	length = doc.evaluate("//video/end-credits-time", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
	type = doc.evaluate("//video/programming-type", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
	genre = doc.evaluate("//show/genre", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
	network = doc.evaluate("//video/channel", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
	userStars = doc.evaluate("//video/user-star-rating", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
	releasedAt = doc.evaluate("//video/released-at", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
	expiresAt = doc.evaluate("//video/expires-at", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
	
	//poster = doc.evaluate("//channel/image/url", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
	
	var dctk = connected.toolkit;
	
	var popupTitleTxt = dctk.getElementById("popupTitleTxt");
	if (main.toLowerCase() != episode.toLowerCase()) {
		popupTitleTxt.text = main+": "+episode;
 		}
 	else 
		popupTitleTxt.text = main;
		
		connected.debug.print("Pop up Main: "+main);
	
	var lengthTime = length.split(";")
	var lengthTxt = dctk.getElementById("lengthTxt");
    lengthTxt.text = lengthTime[0];
	
	var typeTxt = dctk.getElementById("typeTxt");
    typeTxt.text = type;
	
	var genreTxt = dctk.getElementById("genreTxt");
    genreTxt.text = genre;
	
	var networkTxt = dctk.getElementById("networkTxt");
    networkTxt.text = network;
	
	var releasedDate = releasedAt.split("T")
	var releasedTxt = dctk.getElementById("releasedTxt");
    releasedTxt.text = releasedDate[0];
	
	var expiresDate = expiresAt.split("T")
	var expiresTxt = dctk.getElementById("expiresTxt");
    expiresTxt.text = expiresDate[0];
	
	var descTxt = dctk.getElementById("descTxt");
    descTxt.text = mainDesc;
	
	var rating = Math.round((userStars)*Math.pow(10,0))/Math.pow(10,0);
	var stars = dctk.getElementById("stars");
	stars.img.src = rating+".png";
	
	connected.debug.print(rating);

}

function onMetaXHRStateChange()
{
    if (metaXHR.readyState == 4)
    {
        if (metaXHR.status == 200)
        {
            handleMetaResult(metaXHR.responseXML);
        }
    }
}

function startMetaRequest()
{
    metaXHR = new XMLHttpRequest();
    metaXHR.onreadystatechange = onMetaXHRStateChange;
    metaXHR.open("GET", "http://r.hulu.com/videos?eid="+embed, true);
    metaXHR.send(null);
}